package com.burst.reptile.modular.zp.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.UserAgentUtils;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.security.LoginBody;
import com.burst.reptile.framework.security.SignupBody;
import com.burst.reptile.framework.security.service.UserLoginService;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.service.ISysUserService;
import com.burst.reptile.modular.zp.form.NewForgot;

import lombok.RequiredArgsConstructor;

@Controller
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpLoginController extends BaseController{
	
	private final UserLoginService userLoginService;
	
	private final ISysUserService userService;
	
	/**
	 * 登录页面
	 * @return
	 */
	@GetMapping("/login")
	public String login() {
        if (SecurityUtils.isLogin()) {
        	return "redirect:/index";
        }
		return UserAgentUtils.isMobile() ? "H5/login" : "login";
	}
	
	/**
	 * 前端用户账号密码登录登录
	 * @param loginBody
	 * @return
	 */
	@PostMapping("/userLogin")
	@ResponseBody
	public Result<Object> userLogin(@RequestBody LoginBody loginBody) {
		// 生成令牌
		String token = userLoginService.login(loginBody.getUsername(), loginBody.getPassword());
		Map<String, String> map = new HashMap<>();
		map.put(Constants.TOKEN, token);
		return Result.success(map);
	}
	
	/**
	 * 前端用户手机验证码登录登录
	 * @param loginBody
	 * @return
	 */
	@PostMapping("/userCodeLogin")
	@ResponseBody
	public Result<Object> userCodeLogin(@Validated @RequestBody LoginBody loginBody) {
		// 生成令牌
		String token = userLoginService.login(loginBody.getUsername(), loginBody.getCode(), loginBody.getUuid());
		Map<String, String> map = new HashMap<>();
		map.put(Constants.TOKEN, token);
		return Result.success(map);
	}
	
	/**
	 * 用户注册
	 * @param signupBody
	 * @return
	 */
	@PostMapping("/signup")
	@ResponseBody
	@Log(title = "用户注册", businessType = BusinessType.INSERT)
	public Result<Object> signup(@Validated @RequestBody SignupBody signupBody){
		userService.signup(signupBody);
		return SUCCESS;
	}
	
	/**
	 * 找回密码
	 * @return
	 */
	@GetMapping("/newforgot")
	public String newforgot() {
		return UserAgentUtils.isMobile() ? "H5/forgetPass" : "newforgot";
	}
	
	/**
	 * 用户找回密码
	 * @param signupBody
	 * @return
	 */
	@PostMapping("/user/newforgot")
	@ResponseBody
	@Log(title = "用户找回密码", businessType = BusinessType.UPDATE)
	public Result<Object> newforgot(@Validated @RequestBody NewForgot newForgot){
		userService.newforgot(newForgot);
		return SUCCESS;
	}
	
}
